<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Do functionality
 * File: do.php
 * Purpose: Other "logged-in" functionality
 * Todo: Finish writing it
 *
 */

// load required files
require('system/files/global.php');
require('system/external/libraries/NBBC/nbbc.php');

// MAY MOVE THIS LATER TO MAKE THE TITLE MORE PRECISE!
$core_template_system->setvar(array('page_title' => 'Actions',
									'wysiwyg_js' => true));

// show the header loaded
$core_template_system->pparse();

// make sure the user is logged in & wants to do something - otherwise, redirect
if(CoreUserSystem::isLoggedIn()) {
	// is logged in, continue
	switch($_REQUEST['item']) {
		case 'forum':
			// something forum related
			switch($_REQUEST['action']) {
				case 'newtopic':
					// user wants to create a new topic
					// check to see if that forum even exists
					$result = Db_Abstraction::num_rows(Db_Abstraction::query("SELECT `forum_id` FROM `nbb_forums` WHERE `forum_id` = '" . Db_Abstraction::clean($_REQUEST['forum']) . "' LIMIT 1;"));
					
					// any results?
					if($result > 0) {
						// forum exists
						if(isset($_POST['nev_pt_submit'])) {
							// submit topic
							if(!empty($_POST['nev_tt_title']) && !empty($_POST['nbb_data'])) {
								// required items are set
								if(empty($_POST['nev_tt_desc'])) {
									// no subject
									$topic_subject = 'NULL';
								} else {
									// not empty, clean and set
									$topic_subject = "'" . Db_Abstraction::clean(strip_tags($_POST['nev_tt_desc'])) . "'";
								}
								
								// first, enter the post data
								$topic_q = Db_Abstraction::query("INSERT INTO `nbb_topics` (`topic_title`, 
								           			                                        `topic_subject`,
																                            `topic_forum_id`, 
																				 			`topic_starter`)
													   VALUES ('" . Db_Abstraction::clean(strip_tags($_POST['nev_tt_title'])) . "',
													           " . $topic_subject . ",
															   '" . Db_Abstraction::clean($_POST['forum']) . "',
															   '" . CoreUserSystem::getUserId() . "')", true);
															   
								// add the post and posts_text
								$postd_q = Db_Abstraction::query("INSERT INTO `nbb_posts` (`topic_id`, 
								           			                                       `forum_id`,
																                           `post_title`, 
																				 		   `post_subject`,
																						   `post_user_id`)
													   VALUES ('" . $topic_q . "',
													   		   '" . Db_Abstraction::clean($_POST['forum']) . "',
													   		   '" . Db_Abstraction::clean(strip_tags($_POST['nev_tt_title'])) . "',
													           " . $topic_subject . ",
															   '" . CoreUserSystem::getUserId() . "')", true);
								
								// convert into bbcode
								$bbcode = new BBCode;
	
								// set smiley directories  - ADD SMILEY'S IN LATER
								
								// settings
								$bbcode->SetTagMarker('[');
								$bbcode->SetAllowAmpersand(true);
								$bbcode->SetEnableSmileys(false);
								$bbcode->SetDetectURLs(true);
								$bbcode->SetPlainMode(false);
								
								// parse
								$output = $bbcode->Parse($_REQUEST['nbb_data']);
								
								// insert the actual parsed post data
								// when editing - this gets converted back into html
								$postd_text_q = Db_Abstraction::query("INSERT INTO `nbb_posts_text` (`post_text_id`,
																									 `text`)
																				VALUES ('" . $postd_q . "',
																						'" . Db_Abstraction::clean($output) . "')");
								// completed, tell the user
								$core_template_system->setvar(array('inserted_topic' => true));
							
								// render the page
								$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/newtopic.html');
								$core_template_system->pparse();
							} else {
								// some fields are missing
								$core_template_system->setvar(array('error_message' => 'You failed to fill in all the fields, please go back and try again!',
															'error', $core_lang_system->get_var('ERROR', 4),
															'cancel_redirection' => true));
						
								// render the page
								$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
								$core_template_system->pparse();
							}
						} else {
							// show topic form
							$core_template_system->setvar(array('maketopic' => true,
																'forum_id' => $_GET['forum']));
							
							// render the page
							$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/newtopic.html');
							$core_template_system->pparse();
						}
					} else {
						// no such forum
						$core_template_system->setvar(array('error_message' => 'The forum you\'re attempting to post to does not exist or you don\'t have access to!',
															'error', $core_lang_system->get_var('ERROR', 4),
															'cancel_redirection' => true));
						
						// render the page
						$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
						$core_template_system->pparse();
					}

					// end
					break;
				default:
					// nothing to demonstrate, display error
					$core_template_system->setvar(array('error_message' => 'You have not made a valid request, redirecting back...',
														'error', $core_lang_system->get_var('ERROR', 4),
														'back_url', 'index.php'));
					
					// render the page
					$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
					$core_template_system->pparse();
					
					// end
					break;
			}
			
			// end
			break;
		default:
			// nothing to demonstrate, display error
			$core_template_system->setvar(array('error_message' => 'You have not made a valid request, redirecting back...',
												'error', $core_lang_system->get_var('ERROR', 4),
												'back_url', 'index.php'));
			
			// render the page
			$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
			$core_template_system->pparse();
			
			// end
			break;
	}
} else {
	// not logged in, display error
	$core_template_system->setvar(array('error_message' => '<span style="font-weight: normal;">You need to be logged in - <strong>if you are posting a topic or reply</strong> - open a <strong>new tab</strong>, <strong>login</strong> and then press <strong>F5 on this page</strong> to resume that action.</span>',
										'error', $core_lang_system->get_var('ERROR', 4),
										'cancel_redirection' => true));
	
	// render the page
	$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
	$core_template_system->pparse();
}

// show footer
require(BASE_PATH . '/system/files/footer.php');
?>